package py.una.pol.tesis.ii.emergenciasmovil.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;

import py.una.pol.tesis.ii.emergenciasmovil.app.R;
import py.una.pol.tesis.ii.emergenciasmovil.app.adapter.ParienteListAdapter;
import py.una.pol.tesis.ii.emergenciasmovil.app.clases.Pariente;
import py.una.pol.tesis.ii.emergenciasmovil.app.ui.NuevoParienteActivity;

/**
 * Created by Leandro on 13/09/2014.
 */
public class AlarmaFragment extends Fragment implements SearchView.OnQueryTextListener{

    private ListView listViewHist ;
    private ArrayList<Pariente> parienteArray;
    private ParienteListAdapter parienteListAdapter;
    private int posicion;

    private final int MENU_NUEVA_NOTI = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_pariente, container, false);

        parienteArray = new ArrayList<Pariente>();


        listViewHist = (ListView) v.findViewById(R.id.pariente_list);

        parienteArray.add(new Pariente("Leandro","Luque","0972263886", ""));
        parienteArray.add(new Pariente("Cynthia","Meza","0971255517", ""));
        parienteArray.add(new Pariente("Thiago","Luque","0972654232", ""));
        parienteArray.add(new Pariente("Martin","Destefano","0971852369", ""));

        parienteListAdapter = new ParienteListAdapter(this.getActivity(), parienteArray);

        // Assign adapter to ListView
        listViewHist.setAdapter(parienteListAdapter);
        setHasOptionsMenu(true);
        return v;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // action with ID action_refresh was selected
            case R.id.nuevo_pariente:
                Intent appInfo = new Intent(getActivity(), NuevoParienteActivity.class);
                startActivity(appInfo);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_search, menu);
        super.onCreateOptionsMenu(menu, inflater);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        MenuItem newNotiItem = menu.findItem(R.id.nuevo_pariente);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);


        searchView.setOnQueryTextListener(this);
    }


    @Override
    public boolean onQueryTextSubmit(String s) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            parienteListAdapter.filtrar("");
            listViewHist.clearTextFilter();
        } else {
            parienteListAdapter.filtrar(newText.toString());
        }
        return true;
    }
}
